package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import com.qk.vo.user.UserListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    // 三大步骤   ① 拉架子   ② 补全参数和返回值  ③  代码

    //根据部门查询用户
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        return Result.success(userService.listByDept(deptId));
    }

    //查询所有用户
    @GetMapping("/list")
    public Result list() {
        List<UserListVO> users = userService.list();
        return Result.success(users);
    }

    // 根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }

    // 修改用户
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    // 根据id查询用户（回显数据）
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    // 批量删除用户
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids) throws Exception {
        userService.deleteBatch(ids);
        return Result.success();
    }

    // 新增用户
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    // 分页查询用户
    @GetMapping
    public Result queryUserByPage(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.queryUserByPage(dto);
        return Result.success(pageResult);
    }
}